#include "Elf.h"
#include "DestroyGameObjectMessage.h"
#include "Game.h"
#include "AnimationSystem.h"
#include "GameplayState.h"
#include "Player.h"
#include "CreateBulletMessage.h"
#include "Projectile.h"
#include "TileSystem.h"
#include "../SGD Wrappers/SGD_AudioManager.h"

Elf::Elf()
{
	SetCurrAnimation("elfwalking");
	AnimationSystem::GetInstance()->StartPlaying(animInfo);
	AnimationSystem::GetInstance()->StartLooping(animInfo);

	m_hImage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/Graphics/elfTorso.png");
}

Elf::~Elf()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
}

void Elf::Update(float elapsedTime)
{
	Enemy::Update(elapsedTime);
	if (!updating) return;

	shootCooldown += elapsedTime;

	if (m_ptPosition.x > GameplayState::GetInstance()->GetPlayer()->GetPosition().x)
		SetFacing(-1);
	if (m_ptPosition.x <= GameplayState::GetInstance()->GetPlayer()->GetPosition().x)
		SetFacing(1);
}

void Elf::Render()
{
	Enemy::Render();
	float rotation = RotationToPlayer();
	if (!animInfo.GetFlashOn())
	{
		if (facing > 0 && rotation < SGD::PI / 2 && rotation > -SGD::PI / 2)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x - 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, rotation, { 24, 22 });
		else if (facing < 0 && rotation < SGD::PI / 2 && rotation > -SGD::PI / 2)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x + 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, rotation, { 24, 22 }, {}, { -1.0f, 1.0f });
		else if (facing > 0)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x - 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, SGD::PI / 2, { 24, 22 });
		else if (facing < 0)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x + 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, -SGD::PI / 2, { 24, 22 }, {}, { -1.0f, 1.0f });
	}
	else if (animInfo.GetFlashOn())
	{
		if (facing > 0 && rotation < SGD::PI / 2 && rotation > -SGD::PI / 2)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x - 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, rotation, { 24, 22 }, { 255, 255, 0, 0 });
		else if (facing < 0 && rotation < SGD::PI / 2 && rotation > -SGD::PI / 2)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x + 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, rotation, { 24, 22 }, { 255, 255, 0, 0 }, { -1.0f, 1.0f });
		else if (facing > 0)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x - 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, SGD::PI / 2, { 24, 22 }, { 255, 255, 0, 0 });
		else if (facing < 0)
			SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage, { GetWeaponOffset().x + 23 - Game::GetInstance()->GetCameraPos().x, GetWeaponOffset().y - 23 - Game::GetInstance()->GetCameraPos().y }, -SGD::PI / 2, { 24, 22 }, { 255, 255, 0, 0 }, { -1.0f, 1.0f });
	}
}

/*virtual*/ bool Elf::DoAttack()
{
	return ((GameplayState::GetInstance()->GetPlayer()->GetPosition() - m_ptPosition).ComputeLength() < Game::GetInstance()->GetScreenWidth() / 3);
}

/*virtual*/ void Elf::Attack()
{
	GameplayState *gameplay = GameplayState::GetInstance();
	TileSystem* TS = TileSystem::GetInstance();
	SGD::Point playerPos = gameplay->GetPlayer()->GetPosition();

	if (shootCooldown > 0.6f)
	{
		shootCooldown = 0.0f;

		CreateBulletMessage * Msg = new CreateBulletMessage{ this, RotationToPlayer(), false };
		Msg->QueueMessage();
	}
	
	if (TS->CheckCollison(gameplay->GetMapIndex(), SGD::Point((m_ptPosition.x + 8) * facing, m_ptPosition.y + 8)))
		m_vtVelocity.x = -m_vtVelocity.x;
	else if (abs(playerPos.x - m_ptPosition.x) > 150.0f)
		MoveTowardsPlayer();
	else
		m_vtVelocity.x = 0.0f;
}

void Elf::MoveTowardsPlayer()
{
	GameplayState *gameplay = GameplayState::GetInstance();
	SGD::Point playerPos = gameplay->GetPlayer()->GetPosition();

	if (animInfo.GetCurrAnimation() == "elfwalking")
	{
		StartAnimation("elfwalking");
	}

	//Move towards player
	if (playerPos.x < m_ptPosition.x)
		MoveLeft();
	else
		MoveRight();
}

void Elf::HandleCollision(GameObject* pOther)
{
	if (pOther->GetType() == MovingObject::OBJ_BULLET)
	{
		// don't shoot yourself
		if (((Projectile *)pOther)->GetOwner() == this) return;

		// call MovingObjects HandleCollision
		MovingObject::HandleCollision(pOther);
		// play hit sfx
		SGD::AudioManager::GetInstance()->PlayAudio(damageAudio);
	}

	CharacterCollision(pOther);
}
